﻿using System;
using System.Collections.Generic;
using System.Text;
using MySql.Data.MySqlClient;
using Halk.Model;

namespace Halk.Dao
{
    public class DaoSysWeb : DaoBase
    {
        public DaoSysWeb(string connectionString) : base(connectionString)
        {
            ConnectionString = connectionString;
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public SysWebInfo GetModel(string flag)
        {
            var strSql = new StringBuilder();
            strSql.Append("select webid,flag,webname,isactive,sortid from sysweb ");
            strSql.Append(" where flag=?flag ");
            MySqlParameter[] parameters = {
					new MySqlParameter("?flag", MySqlDbType.String,30)			};
            parameters[0].Value = flag;
            var model = new Halk.Model.SysWebInfo();
            using (MySqlDataReader dr = MySqlHelper.ExecuteReader(Connection, System.Data.CommandType.Text, strSql.ToString(), parameters))
            {
                while (dr.Read())
                {
                    if (dr["webflag"] != null)
                    {
                        model.webflag = dr["webflag"].ToString();
                    }
                    if (dr["webname"] != null)
                    {
                        model.webname = dr["webname"].ToString();
                    }
                    if (dr["isactive"] != null && dr["isactive"].ToString() != "")
                    {
                        model.isactive = int.Parse(dr["isactive"].ToString());
                    }
                    if (dr["sortid"] != null && dr["sortid"].ToString() != "")
                    {
                        model.sortid = int.Parse(dr["sortid"].ToString());
                    }
                }
            }
            return model;
        }

        public IEnumerable<SysWebInfo> GetList()
        {
            var strSql = new StringBuilder();
            strSql.Append("select webid,flag,webname,isactive,sortid from sysweb ");
            IList<SysWebInfo> list=new List<SysWebInfo>();
            using (MySqlDataReader dr = MySqlHelper.ExecuteReader(Connection, System.Data.CommandType.Text, strSql.ToString(), null))
            {
                while (dr.Read())
                {
                    var model = new Halk.Model.SysWebInfo();
                    if (dr["webflag"] != null)
                    {
                        model.webflag = dr["webflag"].ToString();
                    }
                    if (dr["webname"] != null)
                    {
                        model.webname = dr["webname"].ToString();
                    }
                    if (dr["isactive"] != null && dr["isactive"].ToString() != "")
                    {
                        model.isactive = int.Parse(dr["isactive"].ToString());
                    }
                    if (dr["sortid"] != null && dr["sortid"].ToString() != "")
                    {
                        model.sortid = int.Parse(dr["sortid"].ToString());
                    }
                    list.Add(model);
                }
            }
            return list;
        }
    }
}
